/**
 * 静态分析所有规则集
 *
 * Created by nongzhizhong on 2022/08/11.
 */
const chai = require('chai');
chai.use(require('chai-http'));
const Administrator = require('./../../../models/administrator');
const common = require('./../../common');
const uri = require('./../uri');
const benchmark = require('./../../../benchmark/static-analyze/ruleSets.json');
module.exports = describe('/GET /static-analyze/rules.json', function () {
  const administrator = new Administrator();
  let newUser;
  function url() {
    return '/static-analyze/rules.json';
  }
  before(async function () {
    await administrator.login();
    newUser = await administrator.createRandomTester();
  });
  after(async function () {
    await administrator.removeUsers([newUser.userId]);
  });
  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');
  // 获取所有规则
  it('it should get all rules', function (done) {
    newUser.login()
      .then(() => {
        chai.request(uri)
          .get(url())
          .set('Authorization', `Bearer ${newUser.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);
            res.body.forEach((ruleSet) => {
              if (!benchmark[ruleSet.ruleSetName]) {
                return;
              }
              common.should.be.equal.ruleSetsList(ruleSet, benchmark[ruleSet.ruleSetName]);
            });
            done();
          });
      }).catch(done);
  });
  // 根据level和languages
  it('it should rules by levels and languages', function (done) {
    newUser.login()
      .then(() => {
        chai.request(uri)
          .get(`${url()}?languages=c&levels=1&levels=2&levels=3`)
          .set('Authorization', `Bearer ${newUser.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);
            res.body.forEach((ruleSet) => {
              if (!benchmark[ruleSet.ruleSetName]) {
                return;
              }
              common.should.be.equal.ruleSetsList(ruleSet, benchmark[ruleSet.ruleSetName]);
            });
            done();
          });
      }).catch(done);
  });
});
